<template>
  <div>
    <el-dialog
      title="查看商品属性"
      :visible.sync="dialog.GoodsAttributeMainShow"
      width="80%"
      append-to-body
    >
      <GoodsAttributeMain
        ref="GoodsAttributeMainShow"
        :isMultiple="true"
        :multipleOptions="{optType: 'show'}"
        @showSelectDialog="showSelectDialog"
        @refresh="refresh"
      />
    </el-dialog>
    <el-dialog
      title="新增商品属性"
      :visible.sync="dialog.GoodsAttributeMainAdd"
      width="80%"
      append-to-body
    >
      <GoodsAttributeMain
        ref="GoodsAttributeMainAdd"
        :isMultiple="true"
        :multipleOptions="{optType: 'add'}"
        @closeSelectDialog="closeSelectDialog"
        @refresh="refresh"
      />
    </el-dialog>
  </div>
</template>

<script>
import { doPostJson } from '@/utils/utils'
import * as ResponseStatus from '@/utils/response-status'
import GoodsAttributeMain from '../GoodsAttribute/GoodsAttributeMain.vue'
export default {
  name: 'GoodsCategoryAttrsDialog',
  components: {
    GoodsAttributeMain
  },
  data() {
    return {
      dialog: {
        GoodsAttributeMainShow: false,
        GoodsAttributeMainAdd: false
      },
      category: {},
      selectedData: []
    }
  },
  computed: {},
  mounted() {},
  methods: {
    initData(category) {
      this.category = category
      this.dialog.GoodsAttributeMainShow = true
      this.$nextTick(() => {
        let goodsAttributeMainShow = this.$refs.GoodsAttributeMainShow
        goodsAttributeMainShow.extraData = category
        goodsAttributeMainShow.getSpecAttrs()
        doPostJson(
        '/goods-category-attribute/admin/all-cond',
        {
          categoryId: this.category.id,
          isActive: 1
        },
        {}
      )
        .then(res => {
          let attributeIds = [0]
          res.data.rows.forEach(row => {
            attributeIds.push(row.attrId)
          })
          goodsAttributeMainShow.resetSearchExcludeProps = ['ids']
          goodsAttributeMainShow.$refs.searchDialog.searchForm.ids = attributeIds
          goodsAttributeMainShow.$refs.quickSearch.searchForm.ids = attributeIds
          goodsAttributeMainShow.searchTable(false, true)
          this.selectedData = res.data.rows
        })
        .catch(err => {})
      })
    },
    showSelectDialog() {
      this.dialog.GoodsAttributeMainAdd = true
      this.$nextTick(() => {
        let goodsAttributeMainAdd = this.$refs.GoodsAttributeMainAdd
        let goodsAttributeTable = goodsAttributeMainAdd.$refs.table
        goodsAttributeTable.selectedData = this.selectedData
        goodsAttributeTable.selectedDataIdProp = 'attrId'
        goodsAttributeTable.extraData.categoryId = this.category.id
        goodsAttributeTable.extraData.categoryTitle = this.category.title
        goodsAttributeMainAdd.searchTable(false, true)
      })
    },
    closeSelectDialog() {
      this.dialog.GoodsAttributeMainAdd = false
    },
    refresh() {
      this.initData(this.category)
    }
  }
}
</script>

<style>
</style>
